package org.mineground.commands.irc;

import org.jibble.pircbot.Colors;
import org.jibble.pircbot.User;
import org.mineground.Main;
import org.mineground.handlers.irc.CommandExecutor;
import org.mineground.handlers.irc.UserLevel;
import org.mineground.player.PlayerLogManager;

public class AddNote implements CommandExecutor {
    @Override
    public void onCommand(User sender, UserLevel level, String channel, String command, String args[]) {
	if (level.compareTo(UserLevel.IRC_OP) < 0) {
	    return;
        }
	

	if (args.length < 2) {
	    Main.getInstance().getIRCHandler().sendMessage(channel, Colors.RED + "* Usage:" + Colors.NORMAL + " !addnote [playername] [reason]");
	    return;
	}
        
        String playerName = args[0];
        String noteText;
        StringBuilder noteTextBuilder = new StringBuilder();
        
        for (int index = 1; index < args.length; index++) {
            noteTextBuilder.append(args[index]);
            noteTextBuilder.append(" ");
        }
        
        noteText = noteTextBuilder.toString().substring(0, noteTextBuilder.toString().length() - 1);
   
	if (!PlayerLogManager.addLogEntry(PlayerLogManager.ACTION_ID_NOTE, playerName, sender.getNick(), noteText)) {
            Main.getInstance().getIRCHandler().sendMessage(channel, Colors.RED + "* Error:" + Colors.NORMAL + " That player has never joined LVM.");
            return;
        }
        
        Main.getInstance().getIRCHandler().sendMessage(channel, Colors.DARK_GREEN + "* Log added.");
    }
}